package com.vue.admin.service.common.mybatis.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.MapperException;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Iterator;
import java.util.Set;

/**
 * @program: vue-admin-service
 * @description:
 * @author: zhangyangyang
 * @create: 2020-04-03 11:20
 **/
public class BaseDefDeleteProvider extends MapperTemplate {
    public BaseDefDeleteProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }
    public String deleteListByPrimaryKey(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.deleteFromTable(entityClass, this.tableName(entityClass)));
        sql.append("<where>");
        Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass);
        Iterator var4 = columnSet.iterator();

        if(columnSet.size()==1){
            while(var4.hasNext()) {
                EntityColumn column = (EntityColumn)var4.next();
                sql.append(column.getColumn()).append(" IN (<trim suffixOverrides=\",\"><foreach collection=\"arg0\"  separator=\",\"  item=\"item\">");
                sql.append("#{item}").append("</foreach></trim>)");
            }
            sql.append("</where>");
        }else{
            //暂不支持联合主键批量物理删除
            throw new MapperException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个主键!");
        }
        return sql.toString();
    }
}
